package com.alix.paymoney.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import java.util.List;

/**
 * com.dafenci.android.teach.base
 * 公司:
 * created by 吴世芊 on 2016/10/9 11:00.
 * updated by 吴世芊 on 2016/12/14 解决设置头布局后的方法布局问题
 * 邮箱:apple9302@126.com
 * 此类功能:
 */

public abstract class BaseAdapter<T> extends RecyclerView.Adapter<ViewHolder> {
    public Context context;
    public List<T> list;
    public int selectPosition = -1;
    private OnItemClickListener onItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;
    private int TYPE_HEADER = 1;

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }


    public interface OnItemLongClickListener {
        void onItemLongClick(int position);
    }

    public BaseAdapter(Context context, List<T> list) {
        this.context = context;
        this.list = list;
    }


    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {

        View view = getLayoutView(parent, viewType);
        final ViewHolder holder = new ViewHolder(view, context);
        setListener(view, holder);
        return holder;
    }

    protected void setListener(View view, final ViewHolder holder) {
        if (onItemClickListener!=null){
            view.getRootView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                setSelectPosition(holder.getAdapterPosition());
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(holder.getAdapterPosition());
                    }
                }
            });
        }
       if (onItemLongClickListener!=null){
           view.getRootView().setOnLongClickListener(new View.OnLongClickListener() {
               @Override
               public boolean onLongClick(View v) {
//                setSelectPosition(holder.getAdapterPosition());
                   if (onItemLongClickListener != null) {
                       onItemLongClickListener.onItemLongClick(holder.getAdapterPosition());
                       return true;
                   }
                   return false;
               }
           });
       }

    }

    public View getView(ViewGroup parent, int layoutId) {
        View view = LayoutInflater.from(context).inflate(layoutId, parent, false);
        return view;
    }

    @Override
    public void onViewAttachedToWindow(ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        /*View itemView = holder.itemView;
        PropertyValuesHolder scaleX=PropertyValuesHolder.ofFloat("scaleX",0,1);
        PropertyValuesHolder scaleY=PropertyValuesHolder.ofFloat("scaleY",0,1);
        PropertyValuesHolder alpha=PropertyValuesHolder.ofFloat("alpha",0,1);
        ObjectAnimator animator=ObjectAnimator.ofPropertyValuesHolder(itemView,scaleX,scaleY,alpha);
        animator.setDuration(1000);
        animator.setInterpolator(new LinearInterpolator());

        animator.start();*/

    }


    public abstract View getLayoutView(ViewGroup parent, int viewType);


    @Override
    public abstract void onBindViewHolder(ViewHolder holder, int position);

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

}
